package controller;

import model.Classes;
import service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

@Controller
public class ClassController{
    private ClassService classService;

    public void setClassService(ClassService classService){
        this.classService=classService;
    }

    @RequestMapping("/getAll_class")
    public ModelAndView getAll()throws Exception{
        ArrayList<Classes> classes=classService.getAllClasses();
        ModelAndView modelAndView=new ModelAndView("show_classes");
        modelAndView.addObject("Class",classes);
        return modelAndView;
    }

    @RequestMapping("/delete_class")
    public String delete(HttpServletRequest request,HttpServletResponse response,
                         RedirectAttributes redirectAttributes)throws Exception{
        String classId= request.getParameter("classId");
        boolean success=classService.deleteClass(classId);

        if(success){
            redirectAttributes.addFlashAttribute("succ", "删除成功！");
            return "redirect:/show_classes.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "删除失败！");
            return "redirect:/show_classes.jsp";
        }
    }

    @RequestMapping("/get_class")
    public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String classId= request.getParameter("classId");
        ArrayList<Classes> classes=classService.getClass(classId);
        ModelAndView modelAndView=new ModelAndView("show_classes1");
        modelAndView.addObject("Class",classes);
        return modelAndView;
    }

    @RequestMapping("/edit_class")
    public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String classId= request.getParameter("classId");
        ArrayList<Classes> classes=classService.getClass(classId);
        ModelAndView modelAndView=new ModelAndView("show_classes");
        modelAndView.addObject("Class",classes);
        return modelAndView;
    }

    @RequestMapping("/doEdit_class")
    public String doEdit(HttpServletRequest request,HttpServletResponse response,
                         String majorId,String clName,int number,RedirectAttributes redirectAttributes) throws Exception{
        String classId= request.getParameter("classId");
        Classes classes=new Classes();
        classes.setClassId(classId);
        classes.setMajorId(majorId);
        classes.setClName(clName);
        classes.setNumber(number);
        boolean succ=classService.editClass(classes);
        if(succ){
            redirectAttributes.addFlashAttribute("succ", "修改成功！");
            return "redirect:/show_classes.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "修改失败！");
            return "redirect:/show_classes.jsp";
        }

    }

    @RequestMapping("/add_class")
    public ModelAndView add() throws Exception{
        ModelAndView modelAndView=new ModelAndView("change_classes");
        return modelAndView;

    }

    @RequestMapping("/doAdd_class")
    public String doAdd(String classId,String majorId,String clName,int number,RedirectAttributes redirectAttributes) throws Exception{
        Classes classes=new Classes();
        classes.setClassId(classId);
        classes.setMajorId(majorId);
        classes.setClName(clName);
        classes.setNumber(number);
        boolean succ=classService.addClass(classes);
        if (succ){
            redirectAttributes.addFlashAttribute("succ", "添加成功！");
            return "redirect:/show_classes.jsp";
        }else {
            redirectAttributes.addFlashAttribute("succ", "添加失败！");
            return "redirect:/show_classes.jsp";
        }
    }
}
